package com.example.easycharge.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.easycharge.annotation.PassToken;
import com.example.easycharge.entity.Customer;
import com.example.easycharge.notRepEntity.LoginData;
import com.example.easycharge.notRepEntity.SignUpData;
import com.example.easycharge.repository.CustomerRepository;
import com.example.easycharge.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/*
    登陆与注册的接口

 */
@Tag(name = "用户的登陆与注册")
@Slf4j
@RestController
@RequestMapping("api")
public class ApiController {
    @Autowired
    private CustomerService customerService;


    @Operation(description = "登录接口，采用token验证")
    @PassToken
    @PostMapping("/login")
    public JSONObject login(@RequestBody LoginData user) {
        return customerService.login(user);
    }

    @Operation(description = "注册接口")
    @PassToken
    @PostMapping("/signup")
    public JSONObject signUp(@RequestBody SignUpData signUpData) {
        return customerService.signUp(signUpData);
    }

    @Operation(description = "查询是否存在相同用户名")
    @PassToken
    @GetMapping("count/{userName}")
    public JSONObject countUserName(@PathVariable("userName") String userName) {
        return customerService.countUserName(userName);
    }

    @Operation(description = "初始化服务器")
    @PutMapping("/init")
    public JSONObject init() {
        return customerService.initServer();
    }
}
